using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;

namespace AliCMS.SubscribeModule.Bll
{
    public partial class Person : DbObjectBase, IPerson
    {
        public Person(int instanceID, string lang, bool checkRight, string database)
            : base(instanceID, lang, checkRight, database)
        {
        }

        public override object ExecuteService(HttpContext context)
        {
            object result = null;

            string operation = context.Request.QueryString["Operation"];
            if (operation == "Add")
            {
                Dictionary<string, object> item = new Dictionary<string, object>();
                string value = null;
                value = context.Request.Form["Name"];
                if (value != null) item[Alias.Person.Name] = value;
                value = context.Request.Form["Identity"];
                if (value != null) item[Alias.Person.Identity] = value;
                value = context.Request.Form["Sex"];
                if (value != null) item[Alias.Person.Sex] = value;
                value = context.Request.Form["Birthday"];
                if (value != null) item[Alias.Person.Birthday] = value;
                value = context.Request.Form["RegionID"];
                if (value != null) item[Alias.Person.RegionID] = value;
                value = context.Request.Form["RegionName"];
                if (value != null) item[Alias.Person.RegionName] = value;
                value = context.Request.Form["IndustryID"];
                if (value != null) item[Alias.Person.IndustryID] = value;
                value = context.Request.Form["IndustryName"];
                if (value != null) item[Alias.Person.IndustryName] = value;
                value = context.Request.Form["TagNames"];
                if (value != null) item[Alias.Person.TagNames] = value;
                value = context.Request.Form["TagInnerIDs"];
                if (value != null) item[Alias.Person.TagInnerIDs] = value;
                value = context.Request.Form["Phone"];
                if (value != null) item[Alias.Person.Phone] = value;
                value = context.Request.Form["Mobile"];
                if (value != null) item[Alias.Person.Mobile] = value;
                value = context.Request.Form["Email"];
                if (value != null) item[Alias.Person.Email] = value;
                value = context.Request.Form["QQ"];
                if (value != null) item[Alias.Person.QQ] = value;
                value = context.Request.Form["Address"];
                if (value != null) item[Alias.Person.Address] = value;
                value = context.Request.Form["Zip"];
                if (value != null) item[Alias.Person.Zip] = value;
                value = context.Request.Form["Homepage"];
                if (value != null) item[Alias.Person.Homepage] = value;
                value = context.Request.Form["Profile"];
                if (value != null) item[Alias.Person.Profile] = value;
                value = context.Request.Form["IsDeleted"];
                if (value != null) item[Alias.Person.IsDeleted] = value;
                value = context.Request.Form["CreatorID"];
                if (value != null) item[Alias.Person.CreatorID] = value;
                value = context.Request.Form["Creator"];
                if (value != null) item[Alias.Person.Creator] = value;
                value = context.Request.Form["CreateTime"];
                if (value != null) item[Alias.Person.CreateTime] = value;
                value = context.Request.Form["UpdatorID"];
                if (value != null) item[Alias.Person.UpdatorID] = value;
                value = context.Request.Form["Updator"];
                if (value != null) item[Alias.Person.Updator] = value;
                value = context.Request.Form["UpdateTime"];
                if (value != null) item[Alias.Person.UpdateTime] = value;

                result = Add(item);
            }
            else if (operation == "Modify")
            {
                int id = Convert.ToInt32(context.Request.QueryString["ID"]);

                Dictionary<string, object> changes = new Dictionary<string, object>();
                string value = null;
                value = context.Request.Form["Name"];
                if (value != null) changes[Alias.Person.Name] = value;
                value = context.Request.Form["Identity"];
                if (value != null) changes[Alias.Person.Identity] = value;
                value = context.Request.Form["Sex"];
                if (value != null) changes[Alias.Person.Sex] = value;
                value = context.Request.Form["Birthday"];
                if (value != null) changes[Alias.Person.Birthday] = value;
                value = context.Request.Form["RegionID"];
                if (value != null) changes[Alias.Person.RegionID] = value;
                value = context.Request.Form["RegionName"];
                if (value != null) changes[Alias.Person.RegionName] = value;
                value = context.Request.Form["IndustryID"];
                if (value != null) changes[Alias.Person.IndustryID] = value;
                value = context.Request.Form["IndustryName"];
                if (value != null) changes[Alias.Person.IndustryName] = value;
                value = context.Request.Form["TagNames"];
                if (value != null) changes[Alias.Person.TagNames] = value;
                value = context.Request.Form["TagInnerIDs"];
                if (value != null) changes[Alias.Person.TagInnerIDs] = value;
                value = context.Request.Form["Phone"];
                if (value != null) changes[Alias.Person.Phone] = value;
                value = context.Request.Form["Mobile"];
                if (value != null) changes[Alias.Person.Mobile] = value;
                value = context.Request.Form["Email"];
                if (value != null) changes[Alias.Person.Email] = value;
                value = context.Request.Form["QQ"];
                if (value != null) changes[Alias.Person.QQ] = value;
                value = context.Request.Form["Address"];
                if (value != null) changes[Alias.Person.Address] = value;
                value = context.Request.Form["Zip"];
                if (value != null) changes[Alias.Person.Zip] = value;
                value = context.Request.Form["Homepage"];
                if (value != null) changes[Alias.Person.Homepage] = value;
                value = context.Request.Form["Profile"];
                if (value != null) changes[Alias.Person.Profile] = value;
                value = context.Request.Form["IsDeleted"];
                if (value != null) changes[Alias.Person.IsDeleted] = value;
                value = context.Request.Form["CreatorID"];
                if (value != null) changes[Alias.Person.CreatorID] = value;
                value = context.Request.Form["Creator"];
                if (value != null) changes[Alias.Person.Creator] = value;
                value = context.Request.Form["CreateTime"];
                if (value != null) changes[Alias.Person.CreateTime] = value;
                value = context.Request.Form["UpdatorID"];
                if (value != null) changes[Alias.Person.UpdatorID] = value;
                value = context.Request.Form["Updator"];
                if (value != null) changes[Alias.Person.Updator] = value;
                value = context.Request.Form["UpdateTime"];
                if (value != null) changes[Alias.Person.UpdateTime] = value;

                Modify(id, changes);
            }
            else if (operation == "Delete")
            {
                string id = context.Request.QueryString["ID"];

                string[] ids = id.Split('|');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (String.IsNullOrEmpty(ids[i]))
                        continue;

                    if (i > 0)
                    {
                        Dal.EndWork();
                        Dal.BeginWork(InstanceID, Lang, CheckRight, String.Empty);
                    }

                    Delete(Convert.ToInt32(ids[i]));
                }
            }
            else if (operation == "Recycle")
            {
                string id = context.Request.QueryString["ID"];

                string[] ids = id.Split('|');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (String.IsNullOrEmpty(ids[i]))
                        continue;

                    Recycle(Convert.ToInt32(ids[i]));
                }
            }
            else if (operation == "Recover")
            {
                string id = context.Request.QueryString["ID"];

                string[] ids = id.Split('|');
                for (int i = 0; i < ids.Length; i++)
                {
                    if (String.IsNullOrEmpty(ids[i]))
                        continue;

                    Recover(Convert.ToInt32(ids[i]));
                }
            }
            else if (operation == "List")
            {
                string value = null;

                int page = -1;
                value = context.Request.QueryString["Page"];
                if (value != null) page = Convert.ToInt32(value);

                int pageSize = -1;
                value = context.Request.QueryString["PageSize"];
                if (value != null) pageSize = Convert.ToInt32(value);

                string condition = context.Request.QueryString["Condition"];
                string sort = context.Request.QueryString["Sort"];

                result = List(page, pageSize, condition, sort);
            }
            else
            {
                throw new Exception("lang_header_param");
            }

            return result;
        }

        public int GetInnerIDFromGuid(Guid guid)
        {
            return Dal.GetInnerIDFromGuid(guid);
        }
        public Guid GetGuidFromInnerID(object id)
        {
            return Dal.GetGuidFromInnerID(id);
        }
        public Dictionary<string, object> Get(object id)
        {
            if (CheckRight)
            {
                Guid guid;
                if (id is Guid)
                    guid = (Guid)id;
                else
                    guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Get", guid);
            }

            return Dal.Get(id);
        }
        public Guid Add(Dictionary<string, object> item)
        {
            if (CheckRight)
            {
                ValidateRight("Add", Guid.Empty);
            }

            if (!item.ContainsKey(Alias.Person.ID) || item[Alias.Person.ID].ToString() == String.Empty)
                item[Alias.Person.ID] = Guid.NewGuid();
            else if (!(item[Alias.Person.ID] is Guid))
                item[Alias.Person.ID] = new Guid(item[Alias.Person.ID].ToString());

            if (item.ContainsKey(Alias.Person.IndustryID))
            {
                string industryID = item[Alias.Person.IndustryID].ToString();
                if (industryID == String.Empty || industryID == Guid.Empty.ToString())
                    item[Alias.Person.IndustryID] = DBNull.Value;
                else
                    item[Alias.Person.IndustryID] = new Guid(industryID);
            }

            if (item.ContainsKey(Alias.Person.RegionID))
            {
                string regionID = item[Alias.Person.RegionID].ToString();
                if (regionID == String.Empty || regionID == Guid.Empty.ToString())
                    item[Alias.Person.RegionID] = DBNull.Value;
                else
                    item[Alias.Person.RegionID] = new Guid(regionID);
            }

            Dictionary<string, string> user = Manager.User;
            if (user != null)
            {
                item[Alias.Person.Creator] = user["Name"];
                item[Alias.Person.CreatorID] = new Guid(user["ID"]);
            }

            if (item.ContainsKey(Alias.Person.CreateTime))
                item[Alias.Person.CreateTime] = Convert.ToDateTime(item[Alias.Person.CreateTime]);
            else
                item[Alias.Person.CreateTime] = DateTime.Now;
            item[Alias.Person.UpdateTime] = DateTime.Now;

            Dal.Add(item);

            return (Guid)item[Alias.Person.ID];
        }
        public void Modify(int id, Dictionary<string, object> changes)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Modify", guid);
            }

            if (changes.ContainsKey(Alias.Person.IndustryID))
            {
                string industryID = changes[Alias.Person.IndustryID].ToString();
                if (industryID == String.Empty || industryID == Guid.Empty.ToString())
                    changes[Alias.Person.IndustryID] = DBNull.Value;
                else
                    changes[Alias.Person.IndustryID] = new Guid(industryID);
            }

            if (changes.ContainsKey(Alias.Person.RegionID))
            {
                string regionID = changes[Alias.Person.RegionID].ToString();
                if (regionID == String.Empty || regionID == Guid.Empty.ToString())
                    changes[Alias.Person.RegionID] = DBNull.Value;
                else
                    changes[Alias.Person.RegionID] = new Guid(regionID);
            }

            Dictionary<string, string> user = Manager.User;
            if (user != null)
            {
                changes[Alias.Person.Updator] = user["Name"];
                changes[Alias.Person.UpdatorID] = new Guid(user["ID"]);
            }

            if (changes.ContainsKey(Alias.Person.UpdateTime))
                changes[Alias.Person.UpdateTime] = Convert.ToDateTime(changes[Alias.Person.UpdateTime]);

            Dal.Modify(id, changes);
        }
        public void Delete(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Delete", guid);
            }

            Dal.Delete(id);
        }
        public void Recycle(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Recycle", guid);
            }

            Dictionary<string, object> changes = new Dictionary<string, object>();
            changes[Alias.Person.IsDeleted] = true;

            Dal.Modify(id, changes);
        }
        public void Recover(int id)
        {
            if (CheckRight)
            {
                Guid guid = Dal.GetGuidFromInnerID(id);
                ValidateRight("Recover", guid);
            }

            Dictionary<string, object> changes = new Dictionary<string, object>();
            changes[Alias.Person.IsDeleted] = false;

            Dal.Modify(id, changes);
        }
        public DataTable List(int page, int pageSize, string condition, string sort)
        {
            if (CheckRight)
            {
                ValidateRight("List", Guid.Empty);
            }

            return Dal.List(page, pageSize, condition, sort);
        }
        public int GetRecordCount(string condition)
        {
            return Dal.GetRecordCount(condition);
        }

        #region Properties
        private Dal.IPerson Dal
        {
            get { return (Dal.IPerson)DalBase; }
        }
        #endregion
    }
}
